/*
@author:shita09
@description:smart gateway third party interface test
*/

#include "sdk.h"
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
regist - regist the device get the session
unregsit - unregist the device clear the session
getdesc - get the floor description file
loaddesc - load the floor description file
showdesc - print the floor description file
lighton
lightoff
lightlnss+
lightlnss-
lighttemp+
lighttemp-
exit - exit the program
*/

void heartbeat_cb(int code) { printf("----code=%d\r\n", code); }

void device_onoffline_handle(unsigned short addr, unsigned char onoffline) {
  if (onoffline)
    printf("%s:addr[%04x] online\r\n", __FUNCTION__, addr);
  else
    printf("%s:addr[%04x] offline\r\n", __FUNCTION__, addr);
}

int device_extdata_handle(unsigned short addr, const char *extdata,
                          unsigned short extdataLen) {
  printf("%s:addr[%04x],extdata[%s],extdataLen[%u]\r\n", __FUNCTION__, addr,
         extdata, extdataLen);
  return SDK_OK;
}

void device_data_handle(unsigned short addr, const int dtype, void *pdata) {
  if (dtype < 1) {
    printf("maddr[%04x],device .\r\n", addr);
    return;
  }
  switch (dtype) {
  case enum_mt_outlet: {
    printf("\r\n###########outlet############\r\n%s:addr[%04x]\r\n",
           __FUNCTION__, addr);
    struct device_smart_outlet_aircond_t *data =
        (struct device_smart_outlet_aircond_t *)pdata;
    if (data->aircond_onoff == 0xFF) {
      printf("swonoff[%d]\r\n", data->swonoff);
    } else {
      printf("swonff:%d\r\n", data->swonoff);
      printf("aincond_onoff:%u\r\n", data->aircond_onoff);
      printf("gear:%u\r\n", data->gear);
      printf("mode:%u\r\n", data->mode);
      printf("overload:%u\r\n", data->overload);
      printf("currenct:%u\r\n", data->currenct);
      printf("temp:%u\r\n", data->temp);
      printf("voltage:%u\r\n", data->voltage);
      printf("wind_direction_leftright:%u\r\n", data->wind_direction_leftright);
      printf("wind_direction_updown:%u\r\n", data->wind_direction_updown);
    }
  } break;

  case enum_mt_sw: {
    printf("\r\n############switchs###########\r\n%s:addr[%04x]\r\n",
           __FUNCTION__, addr);
    struct device_switch_t *data = (struct device_switch_t *)pdata;
    printf("data[%04x]\r\n", data->switchidx);
  } break;

  case enum_mt_cw: {
    printf("\r\n###########colortemp############\r\n%s:addr[%04x]\r\n",
           __FUNCTION__, addr);
    struct device_light_cw_t *data = (struct device_light_cw_t *)pdata;
    printf("onoff:%u\r\n", data->onoff);
    printf("colortemp:%u\r\n", data->colortemp);
    printf("lnss:%u\r\n", data->lnss);
  } break;

  case enum_mt_color: {
    printf("\r\n############rgb###########\r\n%s:addr[%04x]\r\n", __FUNCTION__,
           addr);
    struct device_light_color_t *data = (struct device_light_color_t *)pdata;
    printf("hue:%u\r\n", data->hue);
    printf("saturation:%u\r\n", data->saturation);
    printf("brightness:%u\r\n", data->brightness);
  } break;

  case enum_mt_aircond: {
    printf("\r\n###########aircond############\r\n%s:addr[%04x]\r\n",
           __FUNCTION__, addr);
    struct device_CAC_t *data = (struct device_CAC_t *)pdata;
    printf("onoff:%u\r\n", data->onoff);
    printf("aircondtemp:%u\r\n", data->aircondtemp);
    printf("antifreeze_winddirection:%u\r\n", data->antifreeze_winddirection);
    printf("devType:%u\r\n", data->devType);
    printf("direction:%u\r\n", data->direction);
    printf("error:%u\r\n", data->error);
    printf("mode:%u\r\n", data->mode);
    printf("onoffline:%u\r\n", data->onoffline);
    printf("temp:%u\r\n", data->temp);
    printf("tempInRoom:%u\r\n", data->tempInRoom);
    printf("windspeed:%u\r\n", data->windspeed);
  } break;

  case enum_mt_curtain: {
    printf("\r\n##########curtain#############\r\n%s:addr[%04x]\r\n",
           __FUNCTION__, addr);
    struct device_curtain_t *data = (struct device_curtain_t *)pdata;
    printf("motorOnOff:%u\r\n", data->motorOnOff);
    printf("motorPosition:%u\r\n", data->motorPosition);
    printf("handdrag:%u\r\n", data->handdrag);
    printf("direction:%u\r\n", data->direction);
    printf("angleDirection:%u\r\n", data->angleDirection);
    printf("andglePercentage:%u\r\n", data->andglePercentage);
  } break;

  default:
    break;
  }

  printf("\r\n#######################\r\n");
}

unsigned char authcache[1024] = {0};
unsigned char datacache[1024] = {0};
int main(int argc, char **argv) {
  int heart_code = 0;
  if (argc == 1) {
    printf("%s ipaddr\r\n", argv[0]);
    return 0;
  }
  int addr = 0;
  int subaddr = 0;
  char cl[200] = {0};
  sdk_init(3, "0806d2079f8d4d52a7c5b98a858e66d8", "a16tIKFHsDt",
           "d30f229af235418b85d12267b6c824d3");
  sdk_buffer_set(authcache, sizeof(authcache), datacache, sizeof(datacache));
  printf("targetip[%s]\r\n", argv[1]);
#if 1
  if (sdk_target_ip_set(argv[1], strlen(argv[1])) != SDK_OK) {
    return -1;
  }
#else
  if (sdk_target_ip_port_set(argv[1], strlen(argv[1]), 12000, 12001) !=
      SDK_OK) {
    return -1;
  }
#endif

  sdk_device_onoffline_funcb_regist(device_onoffline_handle);
  sdk_device_extdata_funcb_regist(device_extdata_handle);
  sdk_device_data_funcb_regist(device_data_handle);

  while (1) {
    printf("Enter Command:");
    memset(cl, 0, sizeof(cl));
    int ret = scanf("%s", cl);
    if (ret < 0) {
      printf("%s ipaddr\r\n", argv[0]);
      continue;
    }
    if (strcmp(cl, "exit") == 0) {
      return 0;
    } else if (strcmp(cl, "regist") == 0) {
      sdk_auth_regist_send(1);
    } else if (strcmp(cl, "unregist") == 0) {
      sdk_auth_unregist_send(1);
    } else if (strcmp(cl, "getdesc") == 0) {
      char buffer[500] = {0};
      sdk_auth_get_desction_file(buffer, sizeof(buffer));
      printf("buffer[%s]\r\n", buffer);
    } else if (strcmp(cl, "on") == 0) {
      int swidx = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("swidx=:");
      ret = scanf("%d", &swidx);
      printf("addr[%04x],swidx[%02x]\r\n", addr, swidx);
      sdk_ctl_onoff(addr & 0xFFFF, 1, swidx & 0xFF);
    } else if (strcmp(cl, "off") == 0) {
      int swidx = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("swidx=:");
      ret = scanf("%d", &swidx);
      printf("addr[%04x],swidx[%02x]\r\n", addr, swidx);
      sdk_ctl_onoff(addr & 0xFFFF, 0, swidx & 0xFF);
    } else if (strcmp(cl, "lightcw") == 0) {
      int onoff = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("onoff[0-close 1-open]:");
      ret = scanf("%d", &onoff);
      int colortemp = 0, lnss = 0;
      printf("colortemp[0-100]:");
      ret = scanf("%d", &colortemp);
      printf("lnss[1-100]:");
      ret = scanf("%d", &lnss);
      sdk_light_ctl_cw(addr & 0xFFFF, onoff, lnss, colortemp);
    } else if (strcmp(cl, "lightcolor") == 0) {
      int onoff = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("onoff[0-close 1-open]:");
      ret = scanf("%d", &onoff);
      int hue = 0, saturation = 0, brightness = 0;
      printf("hue[0-360]:");
      ret = scanf("%d", &hue);
      printf("saturation[0-100]:");
      ret = scanf("%d", &saturation);
      printf("brightness[0-100]:");
      ret = scanf("%d", &brightness);
      sdk_light_ctl_color(addr & 0xFFFF, onoff, hue, saturation, brightness);
    } else if (strcmp(cl, "lightsence") == 0) {
      int senceid = 0, onoff = 0;
      ;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("onoff[0-close 1-open]:");
      ret = scanf("%d", &onoff);
      printf("senceid:");
      ret = scanf("%d", &senceid);
      sdk_light_ctl_sence(addr & 0xFFFF, onoff, senceid & 0xFFFF);
    } else if (strcmp(cl, "rgbmode") == 0) {
      int speed = 0, mode = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("mode[1-33]:");
      ret = scanf("%d", &mode);
      printf("speed[1-100]:");
      ret = scanf("%d", &speed);
      sdk_light_ctl_rgb_mode(addr & 0xFFFF, speed, mode);
    } else if (strcmp(cl, "curtain_motor") == 0) {
      int flag = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("open1_close2_stop3=:");
      ret = scanf("%d", &flag);
      sdk_curtain_motor_status_set(addr & 0xFFFF, flag & 0xFF);
    } else if (strcmp(cl, "curtain_motor_abs") == 0) {
      int percentage = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("percentage[0-100]=:");
      ret = scanf("%d", &percentage);
      sdk_curtain_motor_percentage_abs_set(addr & 0xFFFF, percentage & 0xFF);
    } else if (strcmp(cl, "curtain_direction") == 0) {
      int direction = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("direction[0-positive,1-negitive]=:");
      ret = scanf("%d", &direction);
      sdk_curtain_direction_set(addr & 0xFFFF, direction & 0xFF);
    } else if (strcmp(cl, "curtain_handdrag") == 0) {
      int dragFlag = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("dragFlag[0-off,1-on]=:");
      ret = scanf("%d", &dragFlag);
      sdk_curtain_hand_drag(addr & 0xFFFF, dragFlag & 0xFF);
    } else if (strcmp(cl, "curtain_motor_rel") == 0) {
      int incOrdec = 0;
      int percentage = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("incOrdec[1-inc,0-dec]=:");
      ret = scanf("%d", &incOrdec);
      printf("percentage[0-100]=:");
      ret = scanf("%d", &percentage);
      sdk_curtain_motor_percentage_rel_set(addr & 0xFFFF, incOrdec & 0xFF,
                                           percentage & 0xFF);
    } else if (strcmp(cl, "curtain_dimmingLimit") == 0) {
      printf("addr=:");
      ret = scanf("%d", &addr);
      sdk_curtain_dimming_limit_set(addr, 0);
    } else if (strcmp(cl, "curtain_angle_percentage") == 0) {
      int percentage = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("percentage[0-100]=:");
      ret = scanf("%d", &percentage);
      sdk_curtain_angle_control_percentage_set(addr & 0xFFFF,
                                               percentage & 0xFF);
    } else if (strcmp(cl, "curtain_angle_direction") == 0) {
      int direction = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("direction[0-right,1-stop,2-left]=:");
      ret = scanf("%d", &direction);
      sdk_curtain_angle_control_direction_set(addr & 0xFFFF, direction & 0xFF);
    } else if (strcmp(cl, "report") == 0) {
      char msg[100] = "1234567890abcdef";
      printf("addr=:");
      ret = scanf("%d", &addr);
      sdk_cloud_report(addr & 0xFFFF, msg, strlen(msg));
    } else if (strcmp(cl, "hello") == 0) {
      heart_code++;
      sdk_heart_beat(heartbeat_cb, heart_code);
    } else if (strcmp(cl, "CAConoff") == 0) {
      int onoff = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("subaddr=:");
      ret = scanf("%d", &subaddr);
      printf("onoff[0-close 1-open]:");
      ret = scanf("%d", &onoff);
      sdk_aircond_onoff(addr & 0xFFFF, subaddr, onoff);
    } else if (strcmp(cl, "CACtemp") == 0) {
      int temp = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("subaddr=:");
      ret = scanf("%d", &subaddr);
      printf("temp[16-30]:");
      ret = scanf("%d", &temp);
      sdk_aircond_temp(addr & 0xFFFF, subaddr, temp);
    } else if (strcmp(cl, "CACmode") == 0) {
      int mode = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("subaddr=:");
      ret = scanf("%d", &subaddr);
      printf("mode[0-10]:");
      ret = scanf("%d", &mode);
      sdk_aircond_mode(addr & 0xFFFF, subaddr, mode);
    } else if (strcmp(cl, "CACwindspeed") == 0) {
      int speed = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("subaddr=:");
      ret = scanf("%d", &subaddr);
      printf("speed[0-10]:");
      ret = scanf("%d", &speed);
      sdk_aircond_windspeed(addr & 0xFFFF, subaddr, speed);
    } else if (strcmp(cl, "CACantifreeze") == 0) {
      int onoff = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("subaddr=:");
      ret = scanf("%d", &subaddr);
      printf("onoff[0-close 1-open]:");
      ret = scanf("%d", &onoff);
      sdk_aircond_antifreeze(addr & 0xFFFF, subaddr, onoff);
    } else if (strcmp(cl, "CACwinddirection") == 0) {
      int direction = 0;
      printf("addr=:");
      ret = scanf("%d", &addr);
      printf("subaddr=:");
      ret = scanf("%d", &subaddr);
      printf("direction[1-little 2-middle 4-big 7-swing]:");
      ret = scanf("%d", &direction);
      sdk_aircond_wind_direction(addr & 0xFFFF, subaddr, direction);
    }
  }

  return 0;
}
